package com.yuanhua.sim.gateway.controller;


import com.yuanhua.core.base.R;

import com.yuanhua.core.base.entity.MsgReqBO;
import com.yuanhua.sim.gateway.model.PathMsg;
import com.yuanhua.sim.gateway.service.GateWayService;
import com.yuanhua.sim.gateway.utils.IncriAndUniqueID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.zookeeper.KeeperException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author songguoxiang
 * @version 1.0
 * @description: TODO
 * @date 2022/8/1 15:04
 */
@RestController
@CrossOrigin
@RequestMapping("/gateway")
@Api(value = "网关", tags = "网关")
public class GateWayController {

    @Autowired
    private GateWayService gateWayService;

    /**
     * 挑选服务提供给客户端进行连接
     * @author songguoxiang
     * @date: 2022/8/1 15:33
     */
    @ApiOperation("挑选服务提供给客户端进行连接")
    @GetMapping("/path")
    public R<PathMsg> getNettyServerPath() throws KeeperException, InterruptedException {
        //找出zookeeper route下的所有结点，挑选一个返回
        PathMsg pathMsg = gateWayService.getPath();
        if (pathMsg == null) {
            //没有可用的服务
            return  R.fail("服务器繁忙");
        }
        return R.success(pathMsg);
    }

    @PostMapping("/send")
    @ApiOperation("发送消息，发送成功返回消息ID")
    public R<String> sendMsg(@RequestBody MsgReqBO msg){
        long start = System.currentTimeMillis();
        String mid = gateWayService.sendMsg(msg);
        System.out.println("接口耗时——----" +  (System.currentTimeMillis() - start) + "ms");
        return R.success(mid);
    }

}
